<?php
session_start();
//Lưu xuống csdl đã học từ vựng
$idLesson = $_SESSION['idLesson'];

$word = new Word();
$VirtualWord = $word->loadAllWords($idLesson);


///////////////////Xu Ly Random Tu/////////////

$RightWords = $VirtualWord;

$orderRightWord = array_rand($RightWords);
$rightWord = $RightWords[$orderRightWord];
unset($RightWords[$orderRightWord]);

$virtualWord1 = new Word();
$virtualWord2 = new Word();
$ThreeWord = array();
array_push($ThreeWord, $rightWord);

while(count($ThreeWord)<2)
{
    $virtualWord1 = $VirtualWord[array_rand($VirtualWord)];
    if($virtualWord1->id != $rightWord->id )
    {
        array_push($ThreeWord, $virtualWord1);
    }
}

while(count($ThreeWord)<3)
{
    $virtualWord2 = $VirtualWord[array_rand($VirtualWord)];
    if(($virtualWord2->id != $rightWord->id) && ($virtualWord2->id != $virtualWord1->id))
    {
        array_push($ThreeWord, $virtualWord2);
    }
}

$_SESSION["ThreeWord"] = $ThreeWord;
$_SESSION["RightWords"] = $RightWords;
$_SESSION["VirtualWord"] = $VirtualWord;



if ($VirtualWord != false) {
    system_message("Going to test");

    forward(elgg_get_site_url() . "MyCourse/test");
} else {
    register_error("can not going to test");
    forward(REFERER); // REFERER is a global variable that defines the previous page
}
?>
    

